use container_application::ollama::OllamaModel;
use container_application::ollama::OllamaModelInfor;
use container_application::ollama::OllamaModelStatus;
use container_application::ollama::OllamaPullModelStatus;
use datasource_connector::modeling_connector::SimpleMetaConnector;
use datasource_connector::restful::get_class;
use model_graph_types::modeling::system_meta_database;
use tauri::{command, AppHandle, Runtime, State, Window};

use crate::handlers::ContextState;
use crate::handlers::Result;

use model_graph_types::{
    database::DatabaseType,
    modeling::{Class, MetaConnector},
};

use container_application::ollama::{
    ollama_list_local_models, ollama_model_provider, ollama_pull_model, ollama_remove_model,
    ollama_show_model,
};

#[command]
pub async fn ollama_provider<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
) -> Result<String> {
    let context = state.context.clone();
    let provider = ollama_model_provider().await?;
    Ok(provider)
}

#[command]
pub async fn ollama_list<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
    model_provider: String,
) -> Result<Vec<OllamaModel>> {
    let items = ollama_list_local_models(&model_provider).await?;
    Ok(items)
}

#[command]
pub async fn ollama_pull<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
    model_provider: String,
    model: String,
) -> Result<OllamaPullModelStatus> {
    Ok(ollama_pull_model(&model_provider, &model).await?)
}

#[command]
pub async fn ollama_show<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
    model_provider: String,
    model: String,
) -> Result<OllamaModelInfor> {
    Ok(ollama_show_model(&model_provider, &model).await?)
}

#[command]
pub async fn ollama_remove<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
    model_provider: String,
    model: String,
) -> Result<()> {
    Ok(ollama_remove_model(&model_provider, &model).await?)
}
